﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.Entity;
using LEARNREN.COM.BLL;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite
{
    public partial class FeedBackUs : BasePage
    {
        protected Catalogue curCatalogue = null;
        protected IEnumerable<Catalogue> childCatalogueList = null;

        private CatalogueLogic catalogueLogic = null;
        private MessageLogic messageLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Initialize();
            if (!IsPostBack)
            {
                InitializeTitle();
            }
        }

        private void InitializeTitle()
        {
            string title = Request.QueryString["MsgTitle"];
            if (string.IsNullOrEmpty(title) == false)
            {
                this.MsgTitleTextBox.Text = Server.UrlDecode(title);
            }
        }

        private void Initialize()
        {
            string sid = Request.QueryString["id"];
            if (string.IsNullOrEmpty(sid) == false)
            {
                long id = 0;
                if (long.TryParse(sid, out id))
                {
                    curCatalogue = catalogueLogic.GetByIdFromCache(id);
                    childCatalogueList = catalogueLogic.GetChildren(curCatalogue.Id);
                }
                else
                {
                    Response.Redirect("~/");
                }
            }
            else
            {
                Response.Redirect("~/");
            }
        }

        protected override void InitLogic()
        {
            catalogueLogic = new CatalogueLogic();
            messageLogic = new MessageLogic();
        }

        protected override void DisposeLogic()
        {
            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
            if (messageLogic != null)
            {
                messageLogic.Dispose();
                messageLogic = null;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Message msg = new Message();

                msg.Id = messageLogic.GetNewIdentity();
                msg.MsgTitle = Server.HtmlEncode(MsgTitleTextBox.Text);
                msg.MsgPhone = Server.HtmlEncode(MsgPhoneTextBox.Text);
                msg.MsgQQ = Server.HtmlEncode(MsgQQTextBox.Text);
                msg.MsgMail = Server.HtmlEncode(MsgMailTextBox.Text);
                msg.MsgContent = Server.HtmlEncode(MsgContentTextBox.Text);
                msg.CreatedTime = DateTime.Now;
                msg.CatalogueId = curCatalogue.Id;
                msg.MsgHasRe = false;

                messageLogic.Save(msg);

                AddScript("alert('发表留言成功。');location.href='/FeedBackList.aspx?id=" + curCatalogue.Id + "'");
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
            }
        }
    }
}